package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Product;
import cn.wolfcode.cms.query.ProductQueryObject;
import cn.wolfcode.cms.service.IProductService;
import cn.wolfcode.cms.util.JSONResult;
import cn.wolfcode.cms.util.PageResult;
import cn.wolfcode.cms.util.UploadUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

//商品
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ServletContext cxt;

    @Autowired
    private IProductService productService;

    @RequiresPermissions("product:index")
    @PermissionName("商品首页")
    @RequestMapping("")
    public String index() {
        productService.list();
        return "product/list";
    }

    //商品信息列表/带分页
    @RequiresPermissions("product:list")
    @PermissionName("商品首页")
    @RequestMapping("list")
    @ResponseBody
    public Object list(ProductQueryObject qo) {
        PageResult result = productService.query(qo);
        return result;
    }


    @RequestMapping("listView")
    @ResponseBody
    public Object listView() {
        return productService.list();
    }


    //所有商品信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return productService.list();
    }


    //商品添加
    @RequiresPermissions("product:add")
    @PermissionName("商品添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Product entity, MultipartFile pic) {
        JSONResult result = new JSONResult();
        if (pic != null && pic.getSize() > 0) {
            String basePath = cxt.getRealPath("/static/image/upload");
            String imgPath = UploadUtil.upload(pic, basePath);
            entity.setImgPath(imgPath);
        }
        try {
            productService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //商品编辑
    @RequiresPermissions("product:edit")
    @PermissionName("商品编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Product entity, MultipartFile pic) {
        if (entity.getId() != null && pic != null && pic.getSize() > 0) {
            UploadUtil.deleteFile(cxt, entity.getImgPath());
        }

        if (pic != null && pic.getSize() > 0) {
            String basePath = cxt.getRealPath("/static/image/upload");
            String imgPath = UploadUtil.upload(pic, basePath);
            entity.setImgPath(imgPath);
        }

        JSONResult result = new JSONResult();
        try {
            productService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //商品删除
    @RequiresPermissions("product:delete")
    @PermissionName("商品删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    //商品的种类
    @RequestMapping("getTotalNumber")
    @ResponseBody
    public Object getTotalNumber() {
        return productService.getTotalNumber();
    }

    //商品的总成本价
    @RequestMapping("totalAmount")
    @ResponseBody
    public Object totalAmount() {
        return productService.totalAmount();
    }


    //商品的一级查询
    @RequestMapping("getProductByPsName")
    @ResponseBody
    public Object getProductByPsName(String name) {
        return productService.getProductByPsName(name);
    }

    //商品的一级查询
    @RequestMapping("getFirstLevel")
    @ResponseBody
    public Object getFirstLevel() {
        return productService.getFirstLevel();
    }

    //商品
    @RequestMapping("getIntegral")
    @ResponseBody
    public Object getIntegral() {
        return productService.getIntegral();
    }


    //商品的导出
    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response) {

        JSONResult result = new JSONResult();
        try {
            productService.export(response);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("导出失败!");
        }
    }


}
